library(tidyverse)
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
## ── Attaching packages ───────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.1 ✔ dplyr 0.8.1
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ──────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(gganimate)
source('covid_log_log_diff/functions.R')
covidByState <- loadAndFormatNytimesCovidPerState()
covidByState<-covidByState %>%
dplyr::filter(!is.na(newCasesPerDay),
!is.na(cases),
newCasesPerDay > 0,
cases > 0) %>%
dplyr::select(-fips,-prevDate,-prevCases)
snippet<-covidByState %>% group_by(date) %>%
summarize(
state = "USA",
cases = sum(cases),
deaths=sum(deaths),
newCasesPerDay = sum(newCasesPerDay)
)
covidByState = bind_rows(snippet, covidByState)
# create loess-smoothed versions of time series for each state
covidByStateSmoothed <- covidByState %>%
filter(state != "USA") %>%
group_by(state) %>%
do(data.frame(.,
smoothed = 10^predict(loess(log10(newCasesPerDay) ~ log10(cases), data = .), .)))
Single state, highlighted with background
covidByStateSmoothed %>%
filter(state == "New York") %>%
ggplot(aes(x=cases, y=smoothed)) +
geom_line(data = covidByStateSmoothed, aes(group = state), color = "grey") +
geom_line(aes(y = smoothed), color = "red") +
scale_x_log10(label = comma) +
scale_y_log10(label = comma) +
coord_equal() +
theme_minimal() +
labs(x = 'Total confirmed cases',
y = 'New confirmed cases per day',
title = 'Trajectory of COVID-19 cases for New York State',
subtitle = 'Grey lines show other U.S. states')

All states, faceted
ggplot(covidByStateSmoothed, aes(x=cases, y=smoothed, group = state)) +
geom_line(data = covidByStateSmoothed %>% rename(group = state),
aes(x = cases, y = smoothed, group = group), color = "grey") +
geom_line(aes(y = smoothed), color = "red") +
scale_x_log10(label = comma, breaks = c(100, 1000, 100000)) +
scale_y_log10(label = comma) +
coord_equal() +
labs(x = 'Total confirmed cases',
y = 'New confirmed cases per day',
title = 'Trajectory of COVID-19 cases in the U.S.') +
facet_wrap(~ state) +
theme_minimal()

Single state, highlighted with background, animated
covidByStateSmoothed %>%
filter(state == "New York") %>%
ggplot(aes(x=cases, y=smoothed)) +
geom_line(data = covidByStateSmoothed, aes(group = state), color = "grey") +
geom_line(aes(y = smoothed), color = "red") +
geom_label(aes(label = state)) +
geom_text(aes(x = 3e4, y = 1.5, label = date)) +
scale_x_log10(label = comma) +
scale_y_log10(label = comma) +
coord_equal() +
theme_minimal() +
transition_reveal(date) +
labs(x = 'Total confirmed cases',
y = 'New confirmed cases per day',
title = 'Trajectory of COVID-19 cases for New York State',
subtitle = 'Grey lines show other U.S. states')

All states, animated
covidByStateSmoothed %>%
filter(state != "USA") %>%
ggplot(aes(x=cases, y=smoothed, group = state)) +
geom_text(aes(label = state)) +
geom_text(aes(x = 3e4, y = 1.5, label = date)) +
geom_line(aes(y = smoothed)) +
scale_x_log10(label = comma) +
scale_y_log10(label = comma) +
coord_equal() +
transition_reveal(date) +
theme_minimal() +
labs(x = 'Total confirmed cases',
y = 'New confirmed cases per day',
title = 'Trajectory of COVID-19 cases in the U.S.')
